package com.example.myapplication;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.Point;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;

/**
 * Created by 杨劲宇 on 2019/5/14.
 */

public class MyView extends View{
    public static final int RECT=1;
    public static final int PEN=0;
    public static final int CIRLE=2;
    private static final String TAG="MyView";
    private Paint paint=new Paint();
    Path path=new Path();
    private int paintMode=0;
    private Point rectLeftTop=new Point();
    private Point rectRightBottom=new Point();
    private  Point circleCenter=new Point();
    private float radius=0;

    public MyView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        paint.setStrokeWidth(12);
        paint.setAntiAlias(true);
        paint.setDither(true);
        paint.setColor(Color.RED);
        paint.setStyle(Paint.Style.STROKE);
    }
    public void setPaintColor(int color){
        paint.setColor(color);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        switch (paintMode){
            case PEN:
                canvas.drawPath(path,paint);
                break;

            case RECT:
                canvas.drawRect(rectLeftTop.x,rectLeftTop.y,rectRightBottom.x,rectRightBottom.y,paint);
                break;
            case CIRLE:
                canvas.drawCircle(circleCenter.x,circleCenter.y,radius,paint);
                break;
        }
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        switch (paintMode){
            case PEN:
                switch (event.getAction()){
                    case MotionEvent.ACTION_DOWN:
                        path.moveTo(event.getX(),event.getY());
                        invalidate();
                        break;
                        case MotionEvent.ACTION_MOVE:
                            path.lineTo(event.getX(),event.getY());
                            break;

                    case MotionEvent.ACTION_POINTER_UP:
                        break;
                }
            case RECT:
                switch (event.getAction()){
                    case  MotionEvent.ACTION_DOWN:
                        rectLeftTop.x=(int)event.getX();
                        rectLeftTop.y=(int)event.getY();
                        break;
                    case MotionEvent.ACTION_MOVE:
                        rectRightBottom.x=(int)event.getX();
                        rectRightBottom.y=(int)event.getY();
                        invalidate();
                        break;
                    case MotionEvent.ACTION_UP:
                        break;
                }
            case CIRLE:
                switch (event.getAction()){
                    case MotionEvent.ACTION_DOWN:
                        circleCenter.x=(int)event.getX();
                        circleCenter.y=(int)event.getY();
                        break;

                    case MotionEvent.ACTION_MOVE:
                        radius=(float)Math.sqrt(Math.pow(event.getX()-circleCenter.x,2)+
                                Math.pow(event.getY()-circleCenter.y,2));
                        invalidate();
                        break;
                        case MotionEvent.ACTION_UP:
                            break;
                }
        }
        return true;

    }
    public void setPaintMode(int mode){
        paintMode=mode;
    }
}
